<template>
	<view class="content">
		<view class="chooseItem">
			<view class="itemTitle">新密码</view>
			<input type="text" v-model="password" placeholder="请输入新密码" placeholder-style="color:#999999;">
		</view>
		<view class="chooseItem">
			<view class="itemTitle">确认密码</view>
			<input type="text" v-model="passwordAgain" placeholder="请再次输入新密码" placeholder-style="color:#999999;">
		</view>
		<view class="chooseItem">
			<view class="itemTitle">验证码</view>
			<input type="text" v-model="code" placeholder="请输入验证码" placeholder-style="color:#999999;" style="width: 300upx;">
			<view class="goldColor" @click="getCodeFn" v-if="!codeing">获取验证码</view>
			<view class="goldColor" v-if="codeing">{{codeTime}}s</view>
		</view>
		<view class="sureBtn goldBg" @click="sureFn">确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				password:'',
				passwordAgain:'',
				code:'',
				codeing:false,
				codeTime:60,
				timer0:'',
				userInfo:{},
			}
		},
		async onLoad() {
			await this.$onLaunched
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		methods: {
			getCodeFn(){
				uni.showLoading({
					mask:true
				})
				this.$http('/api/user/sendEmailSms',{email:this.userInfo.email},'POST',1).then(res=>{
					this.codeing = true
					uni.showToast({
						title:'发送成功',
						icon:'none'
					})
					uni.hideLoading()
					this.codeTime = 60
					this.timer0 = setInterval(()=>{
						this.codeTime--;
						if(this.codeTime<=0){
							this.codeing = false
							clearInterval(this.timer0)
						}
					},1000)
				})
			},
			sureFn(){
				if(!this.password){
					uni.showToast({
						title:'请填写新密码',
						icon:'none'
					})
					return
				}
				if((!this.passwordAgain)||(this.password!=this.passwordAgain)){
					uni.showToast({
						title:'两次密码不一致',
						icon:'none'
					})
					return
				}
				if(!this.code){
					uni.showToast({
						title:'请填写验证码',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					mask:true
				})
				this.$http('/api/user/updatePassword',{
					newPwd:this.password,
					verificationCode:this.code
				},'POST',1).then(res2=>{
					if(this.timer0){
						clearInterval(this.timer0)
					}
					uni.hideLoading()
					uni.reLaunch({
						url:'/pages/my/login'
					}).then(res=>{
						uni.showToast({
							title:'修改密码成功，请重新登录',
							icon:'none'
						})
					})
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		width: 750upx;
		padding: 40rpx 36upx 0;
		.chooseItem {
			width: 100%;
			height: 98upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28upx;
			background: #FFFFFF;
			border-radius: 20upx;
			padding: 0 30upx;
			margin-bottom: 28upx;
			.itemTitle {
				font-size: 32upx;
				font-weight: bold;
			}
			input {
				height: 98upx;
				line-height: 98upx;
				width: 400upx;
				text-align: right;
			}
		}
		.sureBtn {
			width: 650upx;
			height: 88rpx;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			margin: 60rpx 0 20rpx;
		}
	}
</style>